package bank.server;

import bank.internettoegang.IRemoteListener;
import java.rmi.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

import bank.bankieren.*;
import bank.centraal.ICentraal;
import bank.internettoegang.*;
/**
 * 
 * @author Bart
 */
public class BalieserverApplicatie extends JFrame {

    private static final long serialVersionUID = 3719045408845525383L;
    private String nameBank;
    private ArrayList<IRemoteListener> listeners = new ArrayList<IRemoteListener>();
    private IBalie balie;
        private ICentraal centraal;
        FileInputStream in = null;
    public BalieserverApplicatie(String nameBank) {
        try {
            this.nameBank=nameBank;

            String address = java.net.InetAddress.getLocalHost()
                            .getHostAddress();
            int port = 1099;

            Properties props = new Properties();
            String rmiBalie = address + ":" + port + "/" + nameBank;
            props.setProperty("balie", rmiBalie);

            FileOutputStream out = new FileOutputStream(nameBank+".props");
            props.store(out, null);
            out.close();

//            if (java.rmi.registry.LocateRegistry.getRegistry()==null)
//                            java.rmi.registry.LocateRegistry.createRegistry(port);
            
             in = new FileInputStream("centraal.props");  //Was (bank.getName()+".props")
            props = new Properties();
            props.load(in);
            String rmiCentraal = props.getProperty("centraal");
            in.close();
            centraal = (ICentraal) Naming.lookup("rmi://" + rmiCentraal);
            
            
            balie = new Balie(new Bank(nameBank),centraal);
            
            
            centraal.addListener(balie);
            Naming.rebind(nameBank, balie);
            
            System.out.println("counter of bank bound as " + nameBank);

            init();
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }

    private void init() throws Exception {
        setSize(250, 0);
        setTitle(nameBank+"balie is geopend");
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] arg) throws IOException {
        System.out.print("Voer de banknaam in:");
            String naam = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        System.out.println("bank " + naam + " wordt aangemaakt.");
            new BalieserverApplicatie(naam);
    }
}
